<?php
class CoeventsController extends AppController {

	var $name = 'Coevents';
	var $uses = array('Coevent','Codetail');
	var $helpers = array('Html', 'Form','Text','Csv');
	var $paginate = array('limit' => 50);
	function admin_index() {
		$this->Coevent->recursive = 0;
		$this->paginate=array("order"=>array("Coevent.EventDate"=>"DESC"));
		$this->set('coevents', $this->paginate());
	}
	function admin_update_date(){
		//$this->layout = false;
		//$this->autoRender = false;	
		$coevents=$this->Coevent->find("all",
			array("limit"=>"0,1000","fields"=>array('ComID','delistDate'),"conditions"=>array("Coevent.delistDate !="=>"0000-00-00"),'order'=>"Coevent.ComID ASC"));
		//print_r($coevents);exit;
		//$this->set('coevents',$coevents);
		$i=0;
		foreach($coevents as $coevent){
			$data['Codetail'][$i]['ComID']=$coevent['Coevent']['ComID'];
			$data['Codetail'][$i]['delistDate']=$coevent['Coevent']['delistDate'];
			$i++;			
		}	
		//prin($data);
		$this->loadModel("Codetail");
		$this->Codetail->saveAll($data['Codetail']);
	}
	function admin_search_result($Code=null) {
		if (!empty($this->data) || !empty($Code)) {
			//$this->Coevent->recursive=-1;
			$keyword=!empty($Code)?$Code:$this->data['Coevent']['search_event'];
			$this->paginate=array("conditions"=>array("Coevent.Event like "=>"%".$keyword."%"),"order"=>array("Coevent.Event"=>"ASC"));
			$result=$this->paginate('Coevent');
			
			if(count($result)<1){
			$this->Session->setFlash(__('No data found with the Event <i>"'.$keyword.'"</i>. Please, try again.', true));
			}
			elseif(count($result)==1)
			{
				$this->redirect($_SERVER['HTTP_REFERER']);
				exit;
			}
			$this->set('keyword', $keyword);
			$this->set('coevents', $result);			
		}
		
	}
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid coevent', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('coevent', $this->Coevent->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Coevent->create();
			if ($this->Coevent->save($this->data)) {
			$this->Session->setFlash(__('The coevent has been saved', true));
			if(isset($_SERVER['HTTP_REFERER'])){
				$this->redirect($_SERVER['HTTP_REFERER']);
				}				
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The coevent could not be saved. Please, try again.', true));
			}
		}
		$codetails = $this->Coevent->Codetail->find('list');
		$this->set(compact('codetails'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid coevent', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Coevent->save($this->data)) {
				$this->Session->setFlash(__('The coevent has been saved', true));
				if(isset($_SERVER['HTTP_REFERER'])){
				$this->redirect($_SERVER['HTTP_REFERER']);
				}
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The coevent could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Coevent->read(null, $id);
			
		}	
		$codetails = $this->Coevent->Codetail->find('list');
		$this->set(compact('codetails'));		
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for coevent', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Coevent->delete($id)) {
			$this->Session->setFlash(__('Coevent deleted', true));
			if(isset($_SERVER['HTTP_REFERER'])){
				$this->redirect($_SERVER['HTTP_REFERER']);
				}
				//$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Coevent was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function admin_export($keyword=null)
        {
           	$this->Coevent->recursive=-1;
            // Find fields needed without recursing through associated models
            if(empty($keyword)){
			
				$coevents = $this->Coevent->find(
                'all',
                array('order' => "Coevent.EventID ASC"));
			}else{
			$coevents = $this->Coevent->find(
                'all',array("conditions"=>array("Coevent.Event like "=>"%".$keyword."%")),
                array('order' => "Coevent.EventID ASC"));
			}	//pr($codetails);
            $this->layout = false;
			$this->autoLayout = false;			
			$this->set('coevents',$coevents);
        } 
		function admin_upload_csv()	{
		if (!empty($this->data)) {	
		//pr($this->data);
		if (!empty($this->data['Coevent']['fileName']['name']) && is_uploaded_file($this->data['Coevent']['fileName']['tmp_name']))
			{
				if($this->data['Coevent']['fileName']['type']=="text/x-csv" OR $this->data['Coevent']['fileName']['type']=="application/octet-stream" OR $this->data['Coevent']['fileName']['type']=="application/vnd.ms-excel"){
					//$fileData = fread(fopen(, "r"),$this->data['Coevent']['fileName']['size']);
					$messages = $this->Coevent->import($this->data['Coevent']['fileName']['tmp_name']);
					//debug($messages);
					if(isset($messages['errors']) && count($messages['errors'])>0){
					$msg=implode("<br>",$messages['errors']);
					}else
					{
					$msg=implode("<br>",$messages['messages']);
					}
					//echo $msg;
					$this->Session->setFlash($msg);
				}else{
					$this->Session->setFlash(__('Invalid file type. You must upload a CSV file.', true));
				}
			}
		else{
			$this->Session->setFlash(__('CSV File Required.', true));
		}
		}
	}
}